<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>通知公告</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <link href="/wangeditor-5.1.23/style.css" rel="stylesheet">
    <script src="/wangeditor-5.1.23/index.js" charset="utf-8"></script>
    <style>
        body { background-color: #ffffff; }
        #editor—wrapper {
            border: 1px solid #ccc;
            z-index: 100; /* 按需定义 */
        }
        #toolbar-container { border-bottom: 1px solid #ccc; }
        #editor-container { height: 300px; }
    </style>
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <div class="layui-form ">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;"><legend>通知公告{{def.id?'编辑':'添加'}}</legend></fieldset>


            <div class="layui-form-item">
                <label class="layui-form-label required">标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题" :value="def.title" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">内容</label>
                <input type="hidden" name="content" :value="def.content">
                <div class="layui-input-block">
                    <div id="editor—wrapper">
                        <div id="toolbar-container"><!-- 工具栏 --></div>
                        <div id="editor-container"><!-- 编辑器 --></div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">状态</label>
                <input type="checkbox" name="status" value="1" lay-skin="switch" lay-filter="switchStatus" lay-text="ON|OFF" :checked="def.status==1"/>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序号</label>
                <div class="layui-input-inline">
                    <input type="number" name="sorts" placeholder="请输入标题" :value="def.sorts" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">类型</label>
                <div class="layui-input-inline">
                    <select class="layui-select" name="type" lay-ignore id="noticeTypeSelect">
                        <option value="">请选择</option>
                        <option v-for="(val,key) in noticeTypes" :selected="def.type==key" :value="key">{{val}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn" id="submitBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
var noticeTypes = getEnum('noticeType')
layui.use(['form'], function () {
    var form = layui.form, layer = layui.layer, $ = layui.$;
    new Vue({
        el:"#app",
        data:{
            def:{
                id:null,
                title:"",
                content:"",
                status:1,
                type:0,
                sorts:0,
            },
            noticeTypes:noticeTypes
        },
        methods:{
            initWangEditor:function (defaultHtml) {
                const { createEditor, createToolbar } = window.wangEditor
                const editorConfig = { // 文档地址: https://www.wangeditor.com/v5/menu-config.html#%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%9C%B0%E5%9D%80
                    placeholder: '请输入...',
                    onChange(editor) {
                        const html = editor.getHtml()
                        $('[name=content]').val(html)
                    },
                    MENU_CONF:{
                        uploadImage:{
                            server: '/admin/upload/wangEditor',
                            maxFileSize: 10 * 1024 * 1024, // 10M
                        }
                    }
                }

                const editor = createEditor({
                    selector: '#editor-container',
                    html: defaultHtml,
                    config: editorConfig,
                    mode: 'default', // 'default' or 'simple'
                })

                const toolbarConfig = {}
                const toolbar = createToolbar({
                    editor,
                    selector: '#toolbar-container',
                    config: toolbarConfig,
                    mode: 'default', // or 'simple'
                })
            }
        },
        mounted:function(){
            var _this = this;
                var id = urlParam("id")
                _this.def.id = id;
                if (id){
                    $.get('/admin/notice/'+id,{},function(res){
                        _this.def = res.data;
                        _this.initWangEditor(res.data.content)
                    })
                } else {
                    _this.initWangEditor('');
                }
                var url = null;
                if (id){
                    url = '/admin/notice/'+id
                } else {
                    url = '/admin/notice/add'
                }
                var btn = $('#submitBtn');
                //监听提交
                form.on('submit(saveBtn)', function (data) {
                    btn.attr('disabled',true)
                    btn.addClass('layui-btn-disabled')
                    var loading = layer.load()
                    var formData = data.field;
                    if (id) formData._method = 'put';
                    if (typeof formData.status == "undefined"){
                        formData.status = 0
                    }
                    $.post(url,formData,function (res) {
                        layer.close(loading)
                        btn.removeAttr('disabled')
                        btn.removeClass('layui-btn-disabled')
                        if (res.code ===0 ){
                            layer.msg('操作成功',{icon:6,time:500},function () {
                                parent.location.reload();
                            })
                        } else {
                            layer.msg(res.msg,{icon:5})
                        }
                    })
                    return false;
                });
               $('#noticeTypeSelect').removeAttr('lay-ignore');
                form.render();

        },
        updated:function(){
            form.render();
        }
    })

});



</script>
</body>
</html>